package com.qzimp.fujiancuo.controller;

import com.qzimp.fujiancuo.entity.Reservation;
import com.qzimp.fujiancuo.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reservations")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    // 添加预约
    @PostMapping("/add")
    public String addReservation(@RequestBody Reservation reservation) {
        try {
            reservationService.addReservation(reservation);
            return "预约成功";
        } catch (IllegalArgumentException e) {
            return e.getMessage();  // 返回错误信息：访客不存在
        }
    }

    // 删除预约
    @DeleteMapping("/delete/{reservationId}")
    public String deleteReservation(@PathVariable Long reservationId) {
        reservationService.deleteReservation(reservationId);
        return "删除成功";
    }

    // 更新预约状态
    @PutMapping("/update-status/{reservationId}")
    public String updateReservationStatus(@PathVariable Long reservationId, @RequestParam String status) {
        reservationService.updateReservationStatus(reservationId, status);
        return "预约状态更新成功";
    }

    // 获取某个访客的所有预约
    @GetMapping("/by-visitor/{visitorId}")
    public List<Reservation> getReservationsByVisitorId(@PathVariable Long visitorId) {
        return reservationService.getReservationsByVisitorId(visitorId);
    }
}